//
//  HLHomeCellRetweetView.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/20.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit
import SnapKit

class HLHomeCellRetweetView: UIView {
    
    var retweetViewBottomConstraint: Constraint?
    
    var retweetStatus: HLHomeStatuses?{
        didSet {
            //设置内容
            retweetLabel.text = "@" + "\(retweetStatus?.user?.name  ?? "")" + " \(retweetStatus?.text ?? "")"
            //设置图片
            retweetPicturesView.picturesUrlArr = retweetStatus?.imageURLArr
            //卸载约束
            retweetViewBottomConstraint?.uninstall()
            //根据是否有图片来更改约束
            if let imageURLArr = retweetStatus?.imageURLArr where imageURLArr.count > 0 {
                //有图片
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(homeCellTopViewMargin).constraint
                })
                return
            }
            //没图片
            self.snp_updateConstraints(closure: { (make) -> Void in
                retweetViewBottomConstraint = make.bottom.equalTo(retweetLabel).offset(homeCellTopViewMargin).constraint
            })
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加子控件
        addSubview(retweetLabel)
        addSubview(retweetPicturesView)
        //设置约束
        //转发文本
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(homeCellTopViewMargin)
            make.right.equalTo(self).offset(-homeCellTopViewMargin)
        }
        //图片视图
        retweetPicturesView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetLabel.snp_bottom).offset(homeCellTopViewMargin)
            make.left.equalTo(self).offset(homeCellTopViewMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            retweetViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(homeCellTopViewMargin).constraint
        }
    }
    
    //文本视图
    private lazy var retweetLabel: UILabel = UILabel(title: "我就来看看,还有谁", titleColor: UIColor.lightGrayColor(), titleFont: 14, center: false)
    //配图视图
    private lazy var retweetPicturesView: HLHomeCellPicturesView = HLHomeCellPicturesView()
}
